/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.autoescola.core.web.security;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.Principal;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author leonardo luz fernandes
 * @since 05/11/2010
 * @version 0.1
 */


@WebServlet(name="LogonServlet",urlPatterns={"/autoescola/logonservlet"})
public class LogonServlet extends HttpServlet {


    protected void processRequest(
            HttpServletRequest request ,
            HttpServletResponse response ) throws ServletException , IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try{

            String username = request.getParameter("j_username");
            String password = request.getParameter("j_password");

            System.out.println("Before Login: ");

            System.out.println("Username: " + username );
            System.out.println("Password: " + password );

            String remoteUser = request.getRemoteUser();
            Principal principal = request.getUserPrincipal();
            System.out.println("Remote user: " + remoteUser );
            System.out.println("Principal: " + principal.getName() );

            request.login( username , password );

            System.out.println("Afore Login: ");

            remoteUser = request.getRemoteUser();
            principal = request.getUserPrincipal();
            System.out.println("Remote user: " + remoteUser );
            System.out.println("Principal: " + principal.getName() );

            
        }catch(Exception ex){
            throw new ServletException(ex);
        }finally{
            request.logout();
            out.close();
        }//end catch

    }//end method

}//end klazz
